package top.truehaku.generator.common.util;




import top.truehaku.generator.code.domain.model.code.Code;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class ReflectUtils {

    public static Field[] getFields(Class<?> beanClass) throws SecurityException {
        return getFieldsDirectly(beanClass, true);
    }

    public static Field[] getFieldsDirectly(Class<?> beanClass, boolean withSuperClassFields) throws SecurityException {
        Field[] allFields = null;
        for(Class searchType = beanClass; searchType != null; searchType = withSuperClassFields ? searchType.getSuperclass() : null) {
            Field[] declaredFields = searchType.getDeclaredFields();
            if (null == allFields) {
                allFields = declaredFields;
            } else {
                List<Field> fields = Arrays.asList(allFields);
                fields.addAll(Arrays.asList(declaredFields));
                allFields = (Field[])fields.toArray();
            }
        }
        return allFields;
    }


    public static void main(String[] args) {
        Field[] fields = ReflectUtils.getFields(Code.class);
        System.out.println(fields);
    }

}
